package com.icode.chat.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA.
 * User: ZhongGang
 * Date: 14-8-16
 * Time: 下午5:18
 */
public class WebPathServlet extends HttpServlet {
    private static final String HTTP = "http";
    private static final String WS = "ws";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURL = request.getRequestURL().toString();
        String webSocketURL = requestURL.replace(HTTP, WS);
        String requestURI = request.getRequestURI();
        webSocketURL = webSocketURL.substring(0, webSocketURL.indexOf(requestURI));
        PrintWriter writer = response.getWriter();
        writer.print(webSocketURL);
    }
}
